package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wbs.mapper.TbUserMapper;
import com.wbs.model.TbSeller;
import com.wbs.model.TbSellerExample;
import com.wbs.model.TbUser;
import com.wbs.model.TbUserExample;
import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import com.wbs.service.LoginService;
import com.wbs.vo.SellerVo;
import com.wbs.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.wbs.utils.ShiroUtils.encryptPassword;
import static com.wbs.utils.ShiroUtils.generateSalt;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/2/22 21:25
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result register(UserVO userVO) {
        //先提取验证码
        String captcha = userVO.getCaptcha();

        //判断用户是否已存在
        List<TbUser> tbUsers = tbUserMapper.selectByExample(this.buildExample(userVO));
        if (tbUsers==null) {
            return Result.RESPONSE(ResultEnums.THE_USERNAME_ALREADY_EXISTS);
        }

        //判断验证码
        //redis获取验证码
        String captcha1 = (String) redisTemplate.opsForValue().get("captcha" + userVO.getPhone());
        //验证码是否存在
        if (captcha1==null || captcha1.isEmpty()){
            return Result.RESPONSE(ResultEnums.NEED_GENERATE_CAPTCHA);
        }
        //判断验证码是否正确
        if (!captcha.equals(captcha1)){
            return Result.RESPONSE(ResultEnums.CAPTCHA_ERROR);
        }

        //密码加盐
        String salt = generateSalt(8);
        userVO.setSalt(salt);
        userVO.setPassword(encryptPassword("md5", userVO.getPassword(), salt, 7));

        //创建更新日期
        userVO.setUpdated(new Date());

        TbUser tbUser = this.convertTb(userVO);
        int i = tbUserMapper.insertSelective(tbUser);
        if (i==0){
            return Result.FAIL();
        }

        return Result.SUCCESS();
    }

    //登陆
    @Override
    public TbUser selectUserByUsername(String username) {
        TbUserExample example = new TbUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(example);
        return tbUsers==null||tbUsers.size()==0?null:tbUsers.get(0);
    }

    /*辅助方法----------------------------------------------------------------*/

    //构建条件类
    private TbUserExample buildExample(UserVO userVO) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        //用户名称
        if (!StrUtil.isEmpty(userVO.getUsername())) {
            criteria.andUsernameEqualTo(userVO.getUsername());
        }
        return example;
    }


    //转换为VO
    private UserVO convertVo(TbUser tbUser) {
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(tbUser, userVO);
        return userVO;
    }
    //转换为Tb
    private TbUser convertTb(UserVO userVO) {
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(userVO, tbUser);
        return tbUser;
    }
}
